package com.fenghongzhang.day009;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


/**
 * User 库
 * Person. 表  id ,name,age  -> 开启事务插入5条
 * Student 表 id ,name, stuId. 方法插入
 *
 * Person 原生
 * Student insert.delete
 * 增
 * 删
 * 改
 * 查
 *
 *
 */
public class MainActivity extends AppCompatActivity {
    private Button insert;
    private Button del;
    private Button update;
    private Button select;
    private SQLiteDatabase db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        //数据库对象
        MySqlHelp mySqlHelp = new MySqlHelp(this, "school.db", null, 1);

        //打开一个数据库
         db = mySqlHelp.getReadableDatabase();


         del.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 //delete form student where id = 2
//                 db.delete("student","id = ?",new String[]{"2"});

                 String sql = "delete from student where id = ?";
                 db.execSQL(sql,new Object[]{"3"});
             }
         });

         update.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 //update student set name = "大明" where id = 3
//                 ContentValues contentValues = new ContentValues();
//                 contentValues.put("name","大明");
//                 db.update("student",contentValues,"id = ?",new String[]{"3"});
                 String sql = "update student set name = ? where id = ?";
                 db.execSQL(sql,new Object[]{"大明",3});
             }
         });


        insert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                db.beginTransaction();

                ContentValues contentValues = new ContentValues();
                /**
                 * 1,字段
                 * 2.值
                 */
                contentValues.put("name","小明");
                contentValues.put("age",16);
                db.insert("student",null,contentValues);


                String sql = "update student set name = ? where id = ?";
                db.execSQL(sql,new Object[]{"大明",3});

                db.setTransactionSuccessful();//提交

                db.endTransaction();

//
//                String sql = "insert into student values(null,?,?)";
//                db.execSQL(sql,new Object[]{"呵呵","40"});

            }
        });

        select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //select * from student;
//                Cursor cursor = db.query("student", null, null, null, null, null, null);
//                if (cursor != null){
//                    while(cursor.moveToNext()){
//                        String name = cursor.getString(cursor.getColumnIndex("name"));
//                        String age = cursor.getString(cursor.getColumnIndex("age"));
//
//                        Toast.makeText(MainActivity.this, name, Toast.LENGTH_SHORT).show();
//                    }
//                    cursor.close();
//                }
                String sql = "select * from student";
                Cursor cursor = db.rawQuery(sql, null);
                if (cursor != null){
                    while(cursor.moveToNext()){
                        String name = cursor.getString(cursor.getColumnIndex("name"));
                        String age = cursor.getString(cursor.getColumnIndex("age"));

                        Toast.makeText(MainActivity.this, name, Toast.LENGTH_SHORT).show();
                    }
                    cursor.close();
                }

            }
        });




    }

    private void initView() {
        insert = (Button) findViewById(R.id.insert);
        del = (Button) findViewById(R.id.del);
        update = (Button) findViewById(R.id.update);
        select = (Button) findViewById(R.id.select);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        db.close();
    }
}
